import os
from openmind_hub import OmFileSystem
from openmind_hub import create_repo, delete_repo, upload_file,OmHubHTTPError
import unittest
import shutil
from openmind_hub import RepositoryNotFoundError
from openmind_hub.plugins.openmind.utils._error import BadRequestError



token = '123456789()'
repo_id = "guoxiaozhen2/model_test2024"
license = "mit"

def check_file_exist(path):
    return os.path.exists(path)

class OmFileSystemTests(unittest.TestCase):
    def setUp(self):
        self.fs = OmFileSystem(token=token)
        self.fs.invalidate_cache()

    def test_01_test_resolve_path_model(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_path = self.fs.resolve_path(path=repo_id)
            self.assertEqual(fs_path.repo_type, "model")
            self.assertEqual(fs_path.repo_id, repo_id)
            self.assertEqual(fs_path.revision, "main")
            self.assertEqual(fs_path.path_in_repo, "")
        except:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_02_test_resolve_path_dataset_file(self):
        repo_id = "guoxiaozhen2/dataset_test202409"
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="dataset",
                exist_ok=True,
                fullname=None
            )
            fs_path = self.fs.resolve_path(path=r"datasets/" + repo_id + r"/README.md")
            self.assertEqual(fs_path.repo_type, "dataset")
            self.assertEqual(fs_path.repo_id, repo_id)
            self.assertEqual(fs_path.revision, "main")
            self.assertEqual(fs_path.path_in_repo, "README.md")
        except:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="dataset",
                missing_ok=True
            )

    def test_03_test_resolve_path_noexistrepo(self):
        try:
            self.fs.resolve_path(path=repo_id)
            raise Exception("解析不存在的仓库路径未报错")
        except FileNotFoundError:
            pass

    def test_04_test_invalidate_cache_check(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_ls = self.fs.ls(path=repo_id)
            print(fs_ls)
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )
            self.fs.ls(path=repo_id)
            self.fs.invalidate_cache(path=repo_id)
            self.fs.ls(path=repo_id)
            raise Exception("清除文件系统缓存未生效")
        except RepositoryNotFoundError:
            pass

    def test_05_test_ls_detail(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_ls = self.fs.ls(path=repo_id, detail=True, refresh=False)
            self.assertIsInstance(fs_ls[0], dict)
        except:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_06_test_ls_nodetail(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_ls = self.fs.ls(path=repo_id, detail=False, refresh=False)
            self.assertIsInstance(fs_ls[0], str)
        except:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_07_test_ls_notdir(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.ls(path=repo_id + r"/README.md", detail=False, refresh=False)
            raise Exception("路径为具体文件执行ls未报错")
        except RepositoryNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_08_test_ls_noexist_path(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.ls(path=repo_id + r"/aaa", detail=True, refresh=False)
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_09_test_rm_store_norecursive(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.rm(path=repo_id, recursive=False)
            raise Exception("不递归删除仓库未报错")
        except BadRequestError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_10_test_rm_store_recursive(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/requirements.txt",
                path_in_repo="a/requirements.txt",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_ls = self.fs.ls(path=repo_id, detail=False)
            self.assertIn("models/guoxiaozhen2/model_test2024/a", fs_ls)
            self.fs.rm(path=repo_id + r"/a", recursive=True)
            fs_ls = self.fs.ls(path=repo_id, detail=False)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/a", fs_ls)
        except:
            raise Exception("递归删除仓库报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_11_test_rm_file(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.rm(path=repo_id + r"/README.md", recursive=False)
            fs_ls = self.fs.ls(path=repo_id, detail=False)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/README.md", fs_ls)
        except:
            raise Exception("不递归删除文件未报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_12_test_rm_noexist_path(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.rm(path=repo_id + r"/aaa", detail=True, refresh=False)
        except OmHubHTTPError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_13_test_rm_file_wildcard(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="a/app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/data.py",
                path_in_repo="a/data.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/requirements.txt",
                path_in_repo="a/requirements.txt",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            self.fs.rm(path=repo_id + r"/a/*.py", recursive=False)
            fs_ls = self.fs.ls(path=repo_id + r"/a", detail=False)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/a/app.py", fs_ls)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/a/data.py", fs_ls)
            self.assertIn("models/guoxiaozhen2/model_test2024/a/requirements.txt", fs_ls)
        except:
            raise Exception("不递归删除文件未报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_14_test_ls_refresh(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_ls1 = self.fs.ls(path=repo_id, detail=False, refresh=False)
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_ls2 = self.fs.ls(path=repo_id, detail=False, refresh=False)
            fs_ls3 = self.fs.ls(path=repo_id, detail=False, refresh=True)
            self.assertEqual(fs_ls1, fs_ls2)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/app.py", fs_ls2)
            self.assertIn("models/guoxiaozhen2/model_test2024/app.py", fs_ls3)
        except:
            raise Exception("函数主体运行报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_15_test_glob_wildcard(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="a/app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/data.py",
                path_in_repo="a/b/data.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_glob1 = self.fs.glob(path=repo_id + r"/a/*.py")
            fs_glob2 = self.fs.glob(path=repo_id + r"/**/*.py")
            self.assertIn("models/guoxiaozhen2/model_test2024/a/app.py", fs_glob1)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/a/b/data.py", fs_glob1)
            self.assertIn("models/guoxiaozhen2/model_test2024/a/b/data.py", fs_glob2)
        except:
            raise Exception("函数主体运行报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_16_test_glob_noexistpath(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_glob = self.fs.glob(path=repo_id + r"/**/*.py")
            self.assertEqual([], fs_glob)
        except:
            raise Exception("函数主体运行报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_17_test_glob_noexistrepo(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_glob = self.fs.glob(path=repo_id + r"aaa")
            raise Exception("匹配不存在的仓库的文件未报错")
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_18_test_find_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="a/app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/data.py",
                path_in_repo="a/b/data.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_find1 = self.fs.find(path=repo_id)
            fs_find2 = self.fs.find(path=repo_id + r"/a")
            self.assertIn("models/guoxiaozhen2/model_test2024/README.md", fs_find1)
            self.assertIn("models/guoxiaozhen2/model_test2024/a/app.py", fs_find1)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/README.md", fs_find2)
            self.assertIn("models/guoxiaozhen2/model_test2024/a/b/data.py", fs_find2)
        except:
            raise Exception("查找文件报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_19_test_find_maxdepth(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="a/app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/data.py",
                path_in_repo="a/b/data.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_find1 = self.fs.find(path=repo_id, maxdepth=1)
            fs_find2 = self.fs.find(path=repo_id, maxdepth=3)
            self.assertIn("models/guoxiaozhen2/model_test2024/README.md", fs_find1)
            self.assertNotIn("models/guoxiaozhen2/model_test2024/a/app.py", fs_find1)
            self.assertIn("models/guoxiaozhen2/model_test2024/a/b/data.py", fs_find2)
        except:
            raise Exception("查找文件报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_20_test_find_withdirs(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="a/app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/data.py",
                path_in_repo="a/b/data.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_find1 = self.fs.find(path=repo_id, withdirs=False)
            fs_find2 = self.fs.find(path=repo_id, withdirs=True)
            self.assertNotIn("models/guoxiaozhen2/model_test2024", fs_find1)
            self.assertIn("models/guoxiaozhen2/model_test2024", fs_find2)
        except:
            raise Exception("查找文件报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_21_test_find_file(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.find(path=repo_id + r"/README.md", withdirs=False)
            raise Exception("find查找文件未报错")
        except RepositoryNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_22_test_find_no_existpath(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.find(path=repo_id + r"/aaa", withdirs=False)
            raise Exception("查找不存在的路径目录未报错")
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_23_test_modified_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_modified1 = self.fs.modified(path=repo_id)
            fs_modified2 = self.fs.modified(path=repo_id + r"/README.md")
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            fs_modified3 = self.fs.modified(path=repo_id + r"/app.py")
            self.assertEqual(fs_modified1, fs_modified2)
            self.assertNotEqual(fs_modified1, fs_modified3)
        except:
            raise Exception("获取时间戳有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_24_test_modified_noexist_path(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.modified(path=repo_id + r"/aaa", withdirs=False)
            raise Exception("获取不存在的路径的时间戳未报错")
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_25_test_info_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_info1 = self.fs.info(path=repo_id)
            fs_info2 = self.fs.info(path=repo_id + r"/README.md")
            list1 = ["name", "size", "type", "last_commit"]
            list2 = ["name", "size", "type", "blob_id", "lfs", "last_commit"]
            for i in list1:
                self.assertIn(i, fs_info1)
            for i in list2:
                self.assertIn(i, fs_info2)
        except:
            raise Exception("获取文件（夹）详情有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_26_test_info_noexist_path(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.info(path=repo_id + r"/aaa", withdirs=False)
            raise Exception("获取不存在的路径的详情未报错")
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_27_test_exists_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_exists1 = self.fs.exists(path=repo_id)
            fs_exists2 = self.fs.exists(path=repo_id + r"/aaa")
            self.assertTrue(fs_exists1)
            self.assertFalse(fs_exists2)
        except:
            raise Exception("判断文件（夹）是否存在有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_28_test_isdir_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_isdir1 = self.fs.isdir(path=repo_id)
            fs_isdir2 = self.fs.isdir(path=repo_id + r"/README.md")
            fs_isdir3 = self.fs.isdir(path=repo_id + r"/aaaaa")
            self.assertTrue(fs_isdir1)
            self.assertFalse(fs_isdir2)
            self.assertFalse(fs_isdir3)
        except:
            raise Exception("判断路径是否是文件夹有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_29_test_isfile_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_isfile1 = self.fs.isfile(path=repo_id)
            fs_isfile2 = self.fs.isfile(path=repo_id + r"/README.md")
            fs_isfile3 = self.fs.isfile(path=repo_id + r"/aaa")
            self.assertFalse(fs_isfile1)
            self.assertTrue(fs_isfile2)
            self.assertFalse(fs_isfile3)
        except:
            raise Exception("判断路径是否是文件夹有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_30_test_url_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            fs_url1 = self.fs.url(path=repo_id)
            fs_url2 = self.fs.url(path=repo_id + r"/README.md")
            self.assertEqual("https://openmind.test.osinfra.cn/api/v1/file/guoxiaozhen2/model_test2024/main/media/",fs_url1)
            self.assertEqual("https://openmind.test.osinfra.cn/api/v1/file/guoxiaozhen2/model_test2024/main/media/README.md",fs_url2)
        except:
            raise Exception("判断路径是否是文件夹有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_31_test_get_file_file(self):
        lpath = "/openmind-hub/auto_test/autotest/a.md"
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.get_file(rpath=repo_id+r"/README.md",lpath=lpath)
            a = check_file_exist(lpath)
            self.assertTrue(a)
        except:
            raise Exception("下载文件报错")
        finally:
            os.remove(lpath)
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_32_test_get_file_dir(self):
        lpath = "/openmind-hub/auto_test/autotest/test_openmind_hub/test"
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.get_file(rpath=repo_id,lpath=lpath)
            a = check_file_exist(lpath)
            self.assertTrue(a)
        except:
            raise Exception("下载文件报错")
        finally:
            shutil.rmtree(lpath)
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

    def test_33_test_get_file_noexitst_rpath(self):
        lpath = "/openmind-hub/auto_test/autotest/test"
        try:
            create_repo(
                repo_id=repo_id,
                private=False,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="model",
                exist_ok=True,
                fullname=None
            )
            self.fs.get_file(rpath=repo_id+"/aaa",lpath=lpath)
            raise Exception("下载不存在的文件未报错")
        except FileNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="model",
                missing_ok=True
            )

if __name__ == '__main__':
    unittest.main()
